package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Member;
import com.windfone.model.MemberDetail;
import com.windfone.model.Message;
import com.windfone.model.Order;
import com.windfone.model.ReceiveMsg;
import com.windfone.model.UserSendAddress;
import com.windfone.service.GiftService;
import com.windfone.service.MemberService;
import com.windfone.service.OrderService;

public class MemberCenterAction extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private String newpassword;
	private String password;
	private MemberService memberService;
	private GiftService giftService;
	private OrderService orderService;
	private MemberDetail detailBean;
	private Member bean;
	private List <ReceiveMsg> memberOrders;
	private int chwFlag;
	private String orderSeq;
	private ReceiveMsg receiveMsg;
	private List<Order> orderList ;
	private String qryOrderSeq;
	private String qryBeginDate;
	private String qryEndDate;
	private String buttonType;
	
	private String memo;
	private String detailaddress;
	private String telnumber;
	private String zipcode;
	private String mobile;
	private String name;	
	private String detailAddress;
	private String receiveName;
	private String tel1;
	private String tel2;
	private String tel3;
	private String telNumber;
	private String zipCode;	
	private int addressId;
	private String sex;
	private String email;
	public void setGiftService(GiftService giftService) {
		this.giftService = giftService;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	private int memberId;
	public int getMemberId() {
		return memberId;
	}
	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}
	public ReceiveMsg getReceiveMsg() {
		return receiveMsg;
	}
	public void setReceiveMsg(ReceiveMsg receiveMsg) {
		this.receiveMsg = receiveMsg;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public String getQryOrderSeq() {
		return qryOrderSeq;
	}
	public String getQryBeginDate() {
		return qryBeginDate;
	}
	public String getQryEndDate() {
		return qryEndDate;
	}
	public String getButtonType() {
		return buttonType;
	}
	public void setQryOrderSeq(String qryOrderSeq) {
		this.qryOrderSeq = qryOrderSeq;
	}
	public void setQryBeginDate(String qryBeginDate) {
		this.qryBeginDate = qryBeginDate;
	}
	public String getMemo() {
		return memo;
	}
	public String getDetailaddress() {
		return detailaddress;
	}
	public String getTelnumber() {
		return telnumber;
	}
	public String getZipcode() {
		return zipcode;
	}
	public String getMobile() {
		return mobile;
	}
	public String getName() {
		return name;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	public void setDetailaddress(String detailaddress) {
		this.detailaddress = detailaddress;
	}
	public void setTelnumber(String telnumber) {
		this.telnumber = telnumber;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setQryEndDate(String qryEndDate) {
		this.qryEndDate = qryEndDate;
	}
	public void setButtonType(String buttonType) {
		this.buttonType = buttonType;
	}
	public String getOrderSeq() {
		return orderSeq;
	}
	public void setOrderSeq(String orderSeq) {
		this.orderSeq = orderSeq;
	}
	public int getChwFlag() {
		return chwFlag;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
	public void setChwFlag(int chwFlag) {
		this.chwFlag = chwFlag;
	}
	public MemberDetail getDetailBean() {
		return detailBean;
	}
	public Member getBean() {
		return bean;
	}
	public void setDetailBean(MemberDetail detailBean) {
		this.detailBean = detailBean;
	}
	public List<ReceiveMsg> getMemberOrders() {
		return memberOrders;
	}
	public void setMemberOrders(List<ReceiveMsg> memberOrders) {
		this.memberOrders = memberOrders;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public void setBean(Member bean) {
		this.bean = bean;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public String getPassword() {
		return password;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String   memberCenter() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
	    }
		return SUCCESS;
	}
	
	public String   memberOrderDelete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			orderService.deleteOrderByOrderSeqMemberId(orderSeq, bean.getId());
			orderService.delteReceiveMsgByOrderSeqAndMemberId(orderSeq, bean.getId());
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
		}
		return SUCCESS;
	}
	public String   memberOrderUpdate() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			ReceiveMsg updateReceiveMsg =  orderService.getReceiveMsgByOrderId( orderSeq );
			updateReceiveMsg.setDetailaddress(detailaddress);
			updateReceiveMsg.setMobile(mobile);
			updateReceiveMsg.setName(name);
			updateReceiveMsg.setOrderSeq(orderSeq);
			updateReceiveMsg.setTelnumber(telnumber);
			updateReceiveMsg.setZipcode(zipcode);
			updateReceiveMsg.setUserMemo(memo);
			orderService.updateReceiveMsgByUser(updateReceiveMsg);
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
		}
		return SUCCESS;
	}
 
	private String status;
	private String historyFlag;
	public String getHistoryFlag() {
		return historyFlag;
	}
	public void setHistoryFlag(String historyFlag) {
		this.historyFlag = historyFlag;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String   memberCenterQry() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
//			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
			if ( buttonType.equals("orderseq") ){
				String whereStr = "";
				if ( qryOrderSeq!=null && !qryOrderSeq.equals("") &&  qryOrderSeq.indexOf("*")==-1)
					whereStr = whereStr+" AND  orderSeq LIKE '%"+qryOrderSeq +"%'";
				memberOrders = orderService.getReceiveMsgByUserIdAndCondition( bean.getId(),whereStr );
			}else 	if ( buttonType.equals("date") ){
				String whereStr = "";
				if ( qryBeginDate!=null && !qryBeginDate.equals("") &&  qryBeginDate.indexOf("*")==-1){
					whereStr = whereStr+" AND  createtime>= '"+qryBeginDate +"'";
				}
				if ( qryEndDate!=null && !qryEndDate.equals("") &&  qryEndDate.indexOf("*")==-1){
					whereStr = whereStr+" AND  createtime <= '"+qryEndDate +"'";
				}
				memberOrders = orderService.getReceiveMsgByUserIdAndCondition( bean.getId(),whereStr );
			} else if ( buttonType.equals("orderType")){
				String whereStr = "";
				if ( status !=null && !status.equals("")){
					if ( "6".equals(status)){
						whereStr =  whereStr + " AND status !=5 ";
					} else if ( "5".equals(status)){
						whereStr =  whereStr + " AND status =5 ";
					}
					status = "";
				}
				memberOrders = orderService.getReceiveMsgByUserIdAndCondition( bean.getId(),whereStr );
				if ( historyFlag !=null && !historyFlag.equals("")) {
					List <ReceiveMsg> memberOrders2 = orderService.getAllOrderHistory( bean.getId());
					for( int i=0;i<memberOrders2.size();i++){
						memberOrders.add(  memberOrders2.get(i) );
					}
				}
			}
		} 
		historyFlag = "";
		return SUCCESS;
	}
	
	public String   memberOrderShow() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
			receiveMsg = orderService.getReceiveMsgByOrderId(orderSeq);
			orderList = orderService.getOrderByOrderSeq(orderSeq);
			String goodsName = "";
			for(int i=0;i<orderList.size();i++){
				Order o = orderList.get(i);
				if (i==0)
					goodsName = o.getName();
				else
					goodsName=goodsName+"," +o.getName();
			}
			receiveMsg.setAllGoodsName(goodsName);
		}
		return SUCCESS;
	}
	
	public String   memberInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	bean = memberService.getMemberByKey(bean.getId());
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
	    }
		return SUCCESS;
	}
	

	public String   updateMemberInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			bean = memberService.getMemberByKey(bean.getId());
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			detailBean.setMobileNumber(mobile);
			if ( sex != null && !"".equals(""))
			detailBean.setSex( Integer.parseInt(sex) );
			detailBean.setEmail(email);
			memberService.updateMemberDetail(detailBean);
			bean.setName(name);
			memberService.updateMemberName(bean);
		}
		return SUCCESS;
	}
	

	public int getAddressId() {
		return addressId;
	}
	public void setAddressId(int addressId) {
		this.addressId = addressId;
	}
	private UserSendAddress userSendAddress;
	public UserSendAddress getUserSendAddress() {
		return userSendAddress;
	}
	public void setUserSendAddress(UserSendAddress userSendAddress) {
		this.userSendAddress = userSendAddress;
	}
	public String   memberAddress() throws Exception {
		userSendAddress = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Member bean  = (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			userSendAddress = new UserSendAddress();
			userSendAddress.setMemberid(bean.getId());
			userSendAddress.setId(addressId);
			userSendAddress = memberService.getUserSendAddres(userSendAddress);
		}
		return SUCCESS;
	}
	
	List<UserSendAddress> userSendAddressLs ;
	public List<UserSendAddress> getUserSendAddressLs() {
		return userSendAddressLs;
	}
	public void setUserSendAddressLs(List<UserSendAddress> userSendAddressLs) {
		this.userSendAddressLs = userSendAddressLs;
	}
	public String   memberAddressList() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Member bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			UserSendAddress userSendAddressBean = new UserSendAddress();
			userSendAddressBean.setMemberid(  bean.getId() );
			userSendAddressLs = memberService.listUserSendAddres(userSendAddressBean );
		}
		return SUCCESS;
	}
	
	public String getDetailAddress() {
		return detailAddress;
	}
	public void setDetailAddress(String detailAddress) {
		this.detailAddress = detailAddress;
	}
	public String getReceiveName() {
		return receiveName;
	}
	public void setReceiveName(String receiveName) {
		this.receiveName = receiveName;
	}
	public String getTel1() {
		return tel1;
	}
	public void setTel1(String tel1) {
		this.tel1 = tel1;
	}
	public String getTel2() {
		return tel2;
	}
	public void setTel2(String tel2) {
		this.tel2 = tel2;
	}
	public String getTel3() {
		return tel3;
	}
	public void setTel3(String tel3) {
		this.tel3 = tel3;
	}
	public String getTelNumber() {
		return telNumber;
	}
	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public MemberService getMemberService() {
		return memberService;
	}


	public String   memberUpdateAddress() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Member bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			
			UserSendAddress userSendAddresBean = new UserSendAddress();
			userSendAddresBean.setDetailAddress(detailAddress);
			userSendAddresBean.setMobile(mobile);
			userSendAddresBean.setReceiveName(receiveName);
			userSendAddresBean.setTel1(tel1);
			userSendAddresBean.setTel2(tel2);
			userSendAddresBean.setTel3(tel3);
			userSendAddresBean.setTelNumber(telNumber);
			userSendAddresBean.setZipCode(zipCode);
			userSendAddresBean.setEmail(email);
			userSendAddresBean.setMemberid(bean.getId());
			memberService.updateUserSendAddres(userSendAddresBean );
			
		}
		return SUCCESS;
	}
	
	public String   memberOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
	    }
		return SUCCESS;
	}
	
	public int getBeginRecord() {
		return beginRecord;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public GiftService getGiftService() {
		return giftService;
	}
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private List<Message> memberMsgLs;
	public List<Message> getMemberMsgLs() {
		return memberMsgLs;
	}
	public void setMemberMsgLs(List<Message> memberMsgLs) {
		this.memberMsgLs = memberMsgLs;
	}
	public String   messageList() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			 int beginRecord =0;
			String whereCondition = " AND memberId=" + bean.getId();
			PageUtil pu = new PageUtil();
			pu.setCurrentPage(currentPage);
			int totalRecords = giftService.getAllMessageRecorderNumber(whereCondition) ;
			pu.setTotalRecords( totalRecords );
			pu.setPageSize(Constant.PAGE_SIZE);
			pu.dealPage();
			currentPage = pu.getCurrentPage();
			nextPage = pu.getNextPage();
			totalPage = pu.getTotalPage();
			prevPage = pu.getPrevPage();
			
			beginRecord = Constant.PAGE_SIZE * (currentPage -1);
			if ( beginRecord< 0 )
				beginRecord = 0; 
			memberMsgLs = giftService.getAllMessage( beginRecord,whereCondition);
		}
		return SUCCESS;
	}
	
	private Message msgBean;
	private int msgId;
	public Message getMsgBean() {
		return msgBean;
	}
	public void setMsgBean(Message msgBean) {
		this.msgBean = msgBean;
	}
	public int getMsgId() {
		return msgId;
	}
	public void setMsgId(int msgId) {
		this.msgId = msgId;
	}
	public String   memberMsgDetail() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			
			Message g = new Message() ;
			g.setId(msgId);
			msgBean = giftService.getMessage( g );
			msgBean.setStatus(2);
			giftService.updateMessage(msgBean);
		} else {
			msgBean = null;
		}
		return SUCCESS;
	}
	
	public String   memberMsgDel() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
			
			Message g = new Message() ;
			g.setId(msgId);
			giftService.delMessage( g ) ;
		} else {
			msgBean = null;
		}
		return SUCCESS;
	}
	
	public String   changePwd() throws Exception {
		chwFlag =0;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Member memberBean = (Member)obj;
			bean = (Member)obj;
			bean.setPassword(password);
			if(memberService.checkMemberBypwd(bean)){
				memberBean.setPassword(newpassword);
				chwFlag=memberService.updateMember(memberBean);
			}
		}
		return SUCCESS;
	}
	public String   memberPWD() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
//	    	Member memberBean = (Member)obj;
//	    	memberBean.setPassword(newpassword);
//	    	memberService.updateMember(memberBean);
	    }
		return SUCCESS;
	}

}
